package com.tigerlee.candle.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tigerlee.candle.R;
import com.tigerlee.candle.spreadsheet.GoogleFormSender;
import com.tigerlee.candle.util.Constants;

public class SendPostLocationActivity extends Activity implements
		OnClickListener,  OnFocusChangeListener {

	private EditText mTitleEdit;
	private EditText mNameEdit;
	private EditText mDescriptionEdit;
	private EditText mDateEdit;
	
	private TextView mOkButton;
	private TextView mCancelButton;

	private int mYear;
	private int mMonth;
	private int mDay;
	private static final int SUCCESS_POST = 0;
	private static final int FAIL_POST = 1;
	private static final int TIMEOUT_POST = 2;

	private static final int PROGRESS_DIALOG = 1;
	private static final int DATE_DIALOG = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.send_map_custom_dialog_button);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setupView();
	}

	private void setupView() {
		// get all views in a layout
		mTitleEdit = (EditText) findViewById(R.id.title_edit_sendpost);
		mNameEdit = (EditText) findViewById(R.id.name_edit_sendpost);
		mDescriptionEdit = (EditText) findViewById(R.id.description_edit_sendpost);
		mDateEdit = (EditText) findViewById(R.id.date_edit_sendpost);
		
		mOkButton = (TextView) findViewById(R.id.ok_text_custom_button);
		mCancelButton = (TextView) findViewById(R.id.cancel_text_custom_button);
		
		// set & return color to default
		mOkButton.setBackgroundColor(Color.BLACK);
		mOkButton.setTextColor(Color.WHITE);
		mCancelButton.setBackgroundColor(Color.BLACK);
		mCancelButton.setTextColor(Color.WHITE);
		
		mTitleEdit.requestFocus();
		mOkButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);
		mDateEdit.setOnFocusChangeListener(this);
		
		// get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.ok_text_custom_button:
			v.setBackgroundColor(Color.WHITE);
			mOkButton.setTextColor(Color.BLACK);
			postData();
			break;
		case R.id.cancel_text_custom_button:
			v.setBackgroundColor(Color.WHITE);
			mCancelButton.setTextColor(Color.BLACK);
			setResult(RESULT_CANCELED);
			finish();
			break;
		default:
			break;
		}
	}

	public void postData() {
		Intent i = getIntent();
		ArrayList<String> mArrayList = i
				.getStringArrayListExtra(Intent.EXTRA_TEXT);

		final String mTitle = mTitleEdit.getText().toString();
		final String mName = mNameEdit.getText().toString();
		final String mLatitude = mArrayList.get(0);
		final String mLongitude = mArrayList.get(1);
		final String mDescription = mDescriptionEdit.getText().toString();
		final String mDate = mDateEdit.getText().toString();
		if(mTitle.equals("") || mName.equals("") || mDescription.equals("")){
			Toast.makeText(this, getString(R.string.toast_fill_data),
					Toast.LENGTH_SHORT).show();
			setupView();
			return;
		}else{
			showDialog(PROGRESS_DIALOG);
			mHandler.sendMessage(mHandler.obtainMessage(TIMEOUT_POST));
			new Thread(new Runnable() {
				public void run() {
					try {
						Vector <String> mValueVector = new Vector<String>();
						mValueVector.add(mTitle);
						mValueVector.add(mName);
						mValueVector.add(mDescription);
						mValueVector.add(mLatitude);
						mValueVector.add(mLongitude);
						mValueVector.add(mDate);
						GoogleFormSender mFormSender = new GoogleFormSender(Constants.mMapFormUrl);
						if (mFormSender.send(mValueVector) == true) {
							Intent i = new Intent();
							ArrayList <String> mPassArray = new ArrayList<String>();
							mPassArray.add(mTitle);
							mPassArray.add(mName);
							mPassArray.add(mDescription);
							i.putExtra(Intent.EXTRA_TEXT, mPassArray);
							setResult(RESULT_OK, i);
							mHandler.removeMessages(TIMEOUT_POST);
							mHandler.sendMessage(mHandler
									.obtainMessage(SUCCESS_POST));
						}
					} catch (Exception e) {
						mHandler.removeMessages(TIMEOUT_POST);
						mHandler.sendMessage(mHandler.obtainMessage(FAIL_POST));
					}
				}
			}).start();
		}
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			removeDialog(PROGRESS_DIALOG);
			switch (msg.what) {
			case SUCCESS_POST:
				finish();
				break;
			case FAIL_POST:
			case TIMEOUT_POST:
				setResult(RESULT_CANCELED);
				finish();
				break;
			default:
				break;
			}
		}
	};

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage(getString(R.string.text_sending_dialog));
			dialog.setIndeterminate(true);
			dialog.setCancelable(false);
			return dialog;
		case DATE_DIALOG:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		default:
			break;
		}
		return super.onCreateDialog(id);
	};

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			// Month is 0 based so add 1
			mMonth = monthOfYear+1;
			mDay = dayOfMonth;
			mDateEdit.setText(mDay + "/" + mMonth +"/" + mYear);
		}
	};


	public void onFocusChange(View v, boolean flag) {
		switch (v.getId()) {
		case R.id.date_edit_sendpost:
			if(flag == true){
				showDialog(DATE_DIALOG);
			}
			break;
		default:
			break;
		}
		
	}
}
